//
//  AppDelegate.m
//  ADCDN_APP
//
//  Created by 吃不胖的瘦子 on 2021/2/24.
//

#import "AppDelegate.h"
#if defined(__IPHONE_14_0)
#import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
#endif
@interface AppDelegate ()<ADCDN_SplashAdManagerViewDelegate>
/* 开屏广告对象 */
@property (nonatomic,strong) ADCDN_SplashAdManagerView *splashAdView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 设置window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    ADCDN_ViewController *vc = [[ADCDN_ViewController alloc] init];
    ADCDN_NavigationController * nav = [[ADCDN_NavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    
    
    [self setInit];
    
    
    return YES;
}

#pragma mark - 加载开屏广告
-(void)loadSplashAd{
   // 加载开屏广告
   [self.splashAdView loadSplashAd];
    
}
#pragma mark - 开屏页懒加载
-(ADCDN_SplashAdManagerView *)splashAdView{
    if (!_splashAdView) {
        CGRect frame = [UIScreen mainScreen].bounds;
        _splashAdView = [[ADCDN_SplashAdManagerView alloc] initWithFrame:frame plcId:KplcId_Splash];
        _splashAdView.backgroundColor = [UIColor whiteColor];
        _splashAdView.window = self.window;
        _splashAdView.delegate = self;// manager需要strong持有，否则delegate回调无法执行，影响计费
        [self.window.rootViewController.view addSubview:_splashAdView];
        _splashAdView.rootViewController = self.window.rootViewController;
        // 防止白屏，给一个兜底图，开发者可以设置一个跟启动图一样的
        UIView *bottomView = [[UIView alloc] initWithFrame:frame];
        // icon
        UIImageView *launchImg = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width - 62)/2.0, 180, 62, 62)];
        launchImg.image = [UIImage imageNamed:@"launch_icon"];
        launchImg.contentMode = UIViewContentModeScaleAspectFit;
        [bottomView addSubview:launchImg];
        UIImageView *launchNameImg = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width - 76)/2.0, launchImg.frame.size.height + launchImg.frame.origin.y + 30, 76, 18)];
        launchNameImg.image = [UIImage imageNamed:@"launch_name"];
        launchNameImg.contentMode = UIViewContentModeScaleAspectFit;
        [bottomView addSubview:launchNameImg];
       _splashAdView.bottomView = bottomView;
      
        // 设置开屏底部自定义LogoView，展示半屏开屏广告
        UIView *logoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width * 0.25)];
        UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:logoView.frame];
        CGRect logoFrame = logoImageView.frame;
        logoFrame.size.width = 123;
        logoFrame.size.height = 30;
        logoImageView.frame = logoFrame;
        logoImageView.image = [UIImage imageNamed:@"splash_logo"];
        [logoView addSubview:logoImageView];
        logoImageView.center = logoView.center;
        logoView.backgroundColor = [UIColor whiteColor];
        _splashAdView.logoView = logoView;
    }
    return _splashAdView;
}
/**
 *  开屏广告成功展示
 */
- (void)ADCDN_SplashAdSuccessPresentScreen:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
    NSLog(@"%s---%@",__FUNCTION__,@"开屏广告成功展示");
}
/**
 *  开屏广告展示失败
 */
- (void)ADCDN_SplashAdFailToPresent:(ADCDN_SplashAdManagerView *_Nullable)splashAd withError:(NSError *_Nullable)error {
    NSLog(@"%s---%@ error:%@",__FUNCTION__,@"开屏广告展示失败",error);
    // 移除开屏视图
    [self removeSplashAdView];
}
/**
 *  开屏广告曝光回调
 */
- (void)ADCDN_SplashAdExposured:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
    NSLog(@"%s---%@",__FUNCTION__,@"开屏广告曝光回调");
}
/**
 *  开屏广告点击回调
 */
- (void)ADCDN_SplashAdClicked:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
    NSLog(@"%s---%@",__FUNCTION__,@"开屏广告点击回调");
    // 移除开屏视图
    [self removeSplashAdView];
}
/**
 *  开屏广告关闭回调
 */
- (void)ADCDN_SplashAdClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
    NSLog(@"%s---%@",__FUNCTION__,@"开屏广告关闭回调");
    // 移除开屏视图
    [self removeSplashAdView];
}
/**
 *  开屏广告将要关闭回调
 */
- (void)ADCDN_SplashAdWillClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
    NSLog(@"%s---%@",__FUNCTION__,@"开屏广告将要关闭回调");
    
}
/**
 *  开屏详情页关闭回调
 */
- (void)ADCDN_SplashAdDetailClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
    NSLog(@"%s---%@",__FUNCTION__,@"开屏详情页关闭回调");
}
/**
 *  移除广告视图
 */
- (void)removeSplashAdView {
    if (self.splashAdView) {
        [self.splashAdView removeFromSuperview];
        self.splashAdView = nil;
    }
}
/// 初始化SDK
-(void)setInit{

    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // Tracking authorization completed
        }];
    }
    // 开发ADCDN错误日志,默认不开启
    [ADCDN_DebugLogTool setLogEnable:YES];
    // 初始化配置
    [ADCDN_ConfigManager shareManagerWithAppId:KappId];
    // 加载开屏广告
    [self loadSplashAd];

//    #if ADCDNDev
//    // 弹窗用户输入appid方便测试
//    __weak typeof(self) weakSelf = self;
//    [ADCDN_APPAlertViewTool presentAlertViewWithTitle:@"请输入appId,不输入默认appId=600003" message:@"" text:@"600003" cancelTitle:@"取消" defaultTitle:@"确认" distinct:YES cancel:^{
//        // 开发ADCDN错误日志,默认不开启
//        [ADCDN_DebugLogTool setLogEnable:YES];
//        // 初始化配置
//        [ADCDN_ConfigManager shareManagerWithAppId:KappId];
//        // 加载开屏广告
//        [weakSelf loadSplashAd];
//    } confirm:^(NSString *text) {
//        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
//        if (![text isEqualToString:@""]) {
//            // 开发ADCDN错误日志,默认不开启
//            [ADCDN_DebugLogTool setLogEnable:YES];
//            // 初始化配置
//            [ADCDN_ConfigManager shareManagerWithAppId:text];
//            // 加载开屏广告
//            [self loadSplashAd];
//        }
//        else{
//            // 开发ADCDN错误日志,默认不开启
//            [ADCDN_DebugLogTool setLogEnable:YES];
//            // 初始化配置
//            [ADCDN_ConfigManager shareManagerWithAppId:KappId];
//            // 加载开屏广告
//            [weakSelf loadSplashAd];
//        }
//    }];
//    #elif ADCDNPro
//    // 开发ADCDN错误日志,默认不开启
//    [ADCDN_DebugLogTool setLogEnable:YES];
//    // 初始化配置
//    [ADCDN_ConfigManager shareManagerWithAppId:KappId];
//    // 加载开屏广告
//    [self loadSplashAd];
//    #endif
}



@end
